package com.develop.web.in.service;

import com.develop.configuration.AppContext;
import com.develop.data.loan.model.LoanContract;
import com.develop.data.loan.model.LoanInstallment;
import com.develop.web.in.service.api.LoanRepaymentPlanPreviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class LoanRepaymentPlanHubService implements com.develop.web.in.service.api.LoanRepaymentPlanPreviewService<LoanContract, LoanInstallment> {


    //TODO：待优化，是否通过AOP去切换接口持有的实现类实例对象，动态传入标记好像做不到
    //TODO：初始化Hub类时，就创造一个Map，把实例都取出来，给每个实例都打个注解，自动取出来


    @Override
    public List<LoanInstallment> preview(LoanContract loanContract) {

        String productionNumber = loanContract.getLoanProductionNumber();
        String businessNumber = loanContract.getLoanBusinessNumber();

        String clazzName = MessageFormat.format("com.develop.web.in.service.{0}{1}LoanRepaymentPlanService", productionNumber, businessNumber);
        LoanRepaymentPlanPreviewService<LoanContract, LoanInstallment> service;
        try {
            service = (LoanRepaymentPlanPreviewService<LoanContract, LoanInstallment>) AppContext.getBean(Class.forName(clazzName));
            return service.preview(loanContract);
        } catch (ClassNotFoundException e) {
            log.info("ClassNotFoundException {}", clazzName);
            return null;
        }
    }

    @Override
    public List<List<LoanInstallment>> batchPreview(List<LoanContract> loanContracts) {
        return loanContracts.stream().map(this::preview).collect(Collectors.toList());
    }
}
